#include <algorithm>    // std::sort


inline
segm_img::segm_img()
{
  block_size= 151;
  para1 = 10;
  
}

bool 
compareContourAreas ( std::vector<cv::Point> contour1, std::vector<cv::Point> contour2 ) {
    double i = fabs( contourArea(cv::Mat(contour1)) );
    double j = fabs( contourArea(cv::Mat(contour2)) );
    return ( i < j );
    //http://stackoverflow.com/questions/13495207/opencv-c-sorting-contours-by-their-contourarea
}




inline 
cv::Mat
segm_img::get_contour( const cv::Mat Img_gray)
{
  //cout << "Entra Aqui?  " << endl;
  //Img_gray = equalizeImage(Img_gray);
    
  //cv::imshow("input", Img_gray)	;
  
  double img_area =  Img_gray.rows*Img_gray.cols;

  cv::Mat Img_Contours;
  Img_gray.copyTo(Img_Contours);
  cv::GaussianBlur(Img_Contours,Img_Contours,cv::Size(3,3),1.5,1.5);
  cv::adaptiveThreshold( Img_Contours, Img_Contours, 255, 1,1,block_size,para1 );
  
  
  vector< vector<cv::Point> > contours;
  //vector< vector<cv::Point> > contours2;
  cv::findContours( Img_Contours, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE );
  
 
  
  //vector< vector<cv::Point> >::const_iterator itc= contours.begin();
  //cv::Mat mask(Img_gray.size(),CV_8UC3,cv::Scalar(0,0,0));

  cv::Mat mask(Img_gray.size(),CV_8U,cv::Scalar(0));

  double cmin = 20;
  double cmax = img_area*0.5;
  
  
 for(int i = 0; i < contours.size(); i++)
 {///cambiar
 
   double con_area = fabs( contourArea(cv::Mat( contours[i] )) );
   
    //cout << con_area  << " ";
    if (con_area > cmin && con_area < cmax)
    {
      //cout << con_area  << endl;
       cv::drawContours(mask, contours, i, cv::Scalar(255), CV_FILLED); 
       //cv::imshow("mask", mask);  
	//cv::waitKey(); 
    }
    //cv::waitKey(); 
  }
  
  
  //cv::waitKey();
  
  
/*
  // Graficando los 3 mas grandes
  if (contours.size()==1)
  {
     cv::drawContours(mask, contours, -1, cv::Scalar(255), CV_FILLED); 
  }
  
  if (contours.size()>1)
  {
    //cout << "Cuando entra aqui?? " <<contours.size()<<  endl;
    // sort contours
    std::sort(contours.begin(), contours.end(), compareContourAreas);
    // grab contours
    //std::vector<cv::Point> biggestContour1 = contours[contours.size()-1];
    //std::vector<cv::Point> biggestContour2 = contours[contours.size()-2];
    
    cv::drawContours(mask, contours, contours.size()-1, cv::Scalar(255), CV_FILLED); 
    
    double i = fabs( contourArea(cv::Mat( contours[contours.size()-1] )) );
    double j = fabs( contourArea(cv::Mat( contours[contours.size()-2])) );
        
    if ( i>=5*j )
    {
      cv::drawContours(mask, contours, contours.size()-2, cv::Scalar(255), CV_FILLED); 
      if (contours.size()>2)
      {
	cv::drawContours(mask, contours, contours.size()-3, cv::Scalar(255), CV_FILLED); 
	
      }
      
    }
  }
  */
  
  

  //while (itc!=contours.end()) {
      //cv::Rect r0= cv::boundingRect(cv::Mat(*itc ));
      //cv::rectangle(mask,r0,cv::Scalar(255,0,0),2);
      //++itc;
      //cv::imshow("mask", mask);  
      //cv::waitKey();
    
  //}
  

  
  
  //cout << "Aqui?  " << endl;
  cv::Mat Img_crop(Img_gray.size(),CV_8U,cv::Scalar(255));
  //cout << "Aqui 2 ?  " << endl;

  Img_gray.copyTo(Img_crop, mask);
  //cout << "Aqui 3 ?  " << endl;

  
  //cv::imshow("mask", mask);  
  //cv::imshow("cropped", Img_crop);
  //cout << "Aqui 4?  " << endl;
  //cv::waitKey();

  //cv::waitKey();
  
  return Img_crop;
}


/*
 //http://docs.opencv.org/doc/tutorials/imgproc/histograms/histogram_equalization/histogram_equalization.html
inline 
cv::Mat
segm_img::equalizeImage( cv::Mat image)
{
  cv::Mat equ_Img;
 char* source_window = "Source image";
 char* equalized_window = "Equalized Image";


  
  /// Apply Histogram Equalization
  cv::equalizeHist( image, equ_Img );

  /// Display results
  cv::namedWindow( source_window, CV_WINDOW_AUTOSIZE );
  cv::namedWindow( equalized_window, CV_WINDOW_AUTOSIZE );

  cv::imshow( source_window, image );
  cv::imshow( equalized_window, equ_Img );

  /// Wait until user exits the program
  cv::waitKey(0);

  return equ_Img;

}

*/